﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace XBoshiGFS.forms
{
    public partial class WaterSeasonForm : Form
    {

        XBoshiGFSModelContainer context = new XBoshiGFSModelContainer();

        public WaterSeasonForm()
        {
            InitializeComponent();

            waterSeasonSetBindingSource.DataSource = GetQuerySQl();
            feeStandardSetBindingSource.DataSource = (from p in context.FeeStandardSet select p);
        }

        private void waterSeasonSetDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        IList   GetQuerySQl() {
            var sql = (from p in context.WaterSeasonSet  
                       orderby p.strYear descending, p.strSeason descending 
                       select p
                    );
            return sql.ToList() ;
        }


 

        private void btnNew_Click(object sender, EventArgs e)
        {
            WaterSeasonSet wss = new WaterSeasonSet();
            wss.Id = Guid.NewGuid().ToString();
            wss.strYear = String.Format("{0:yyyy}", DateTime.Now);
            wss.strState = "新建";
            wss.strName = "新建灌季";
            context.AddToWaterSeasonSet(wss);
            context.SaveChanges();
            waterSeasonSetBindingSource.DataSource = GetQuerySQl(); 
        }

        private void waterSeasonSetDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {

            BindingSource bins = (BindingSource)waterSeasonSetDataGridView.DataSource;
            WaterSeasonSet season = (WaterSeasonSet)bins.Current;
            if (season.strState == "新建") { 
            } else {
                e.Cancel = true;
                return;
            }

            if (waterSeasonSetDataGridView.Columns[e.ColumnIndex].DataPropertyName   == "strState") {

                e.Cancel = true;
                return;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            context.SaveChanges();
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "确定开始灌季，灌季开始后将不能进行修改！", "提示", MessageBoxButtons.YesNo) == DialogResult.No) {
                //不进行初始化。
                return;
            }

            //惊醒数据初始化
            BindingSource bins = (BindingSource)waterSeasonSetDataGridView.DataSource;
            WaterSeasonSet season = (WaterSeasonSet)bins.Current;
            if (season.strState != "新建")
            {
                MessageBox.Show(this, "当前灌季不是新建灌季，不能初始化！");
                return ;
            }

            if (season.FeeStandardSetId  == null)
            {
                MessageBox.Show(this, "当前灌季没有指定水费，不能初始化！");
                return;
            }

            //进行初始化

            var peopleset=(from p in context.PeopleSet select p) ;
            foreach(var people in peopleset ){
                WaterUsageSet  wus=new WaterUsageSet ();
                wus.Id=Guid.NewGuid ().ToString ();
                context.AddToWaterUsageSet(wus );  
                wus.PeopleId =people.Id;
                wus.UseArea =people.UseArea ;
                wus.WaterSeasonId =season.Id;
                wus.FeeStandardId = season.FeeStandardSetId; 
            
            } 
            season.strState = "灌季中";
            //保存数据
            context.SaveChanges ();     



        }

        private void btnClose_Click(object sender, EventArgs e)
        {

                this.Close();

        }

        private void btnFinish_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show(this, "结束灌季，灌季结束后，将不能进行数据录入！", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                //不进行灌季结束。
                return;
            }

            //惊醒数据初始化
            BindingSource bins = (BindingSource)waterSeasonSetDataGridView.DataSource;
            WaterSeasonSet season = (WaterSeasonSet)bins.Current;
            if (season.strState != "灌季中")
            {
                MessageBox.Show(this, "当前灌季不是进行中灌季，不能灌季结束！");
                return;
            }

            season.strState = "灌季已结束";
            //保存数据
            context.SaveChanges();     



        }
    }
}
